﻿using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using Business.DbConnect;
using PagedList;

namespace ServerMotorManagement.Controllers
{
    [Authorize]
    public class MotorBikeController : Controller
    {
        //
        // GET: /MotorBike/
        /// <summary>
        /// return list
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int? page)
        {
            List<Motorbike> motorbikes = MotorBusiness.SelectAll();
            int pageNumber = (page ?? 1);
            return View(motorbikes.ToPagedList(pageNumber, 3));
        }

        public ActionResult GetList(int? page)
        {
            List<Motorbike> motorbikes = MotorBusiness.SelectAll();
            int pageNumber = (page ?? 1);
            return PartialView("_ListMotor", motorbikes.ToPagedList(pageNumber, 3));
        }

        public ActionResult Create()
        {
            SelectList sList = new SelectList(MotorTypeBusiness.ListMotorTypes(), "Id", "Description");
            ViewBag.MotorType = sList;
            return PartialView("CreateForm");
        }

        [HttpPost]
        public ActionResult Create(Motorbike motorbike)
        {
            bool result = false;
            if (ModelState.IsValid)
            {
                result = MotorBusiness.Insert(motorbike);
            }
            //if (result)
            //{
            return RedirectToAction("Index");
            //Should Process more carefully!!!
            //}
            //else
            //{
            //    //raise system message and still here
            //}
            //return View();
        }

        public ActionResult Edit(string plate)
        {
            Motorbike moTor = MotorBusiness.Select(plate);
            ViewBag.MotorType = new SelectList(MotorTypeBusiness.ListMotorTypes(), "Id", "Description", moTor.MotorType);
            SelectList sList = new SelectList(MotorTypeBusiness.ListMotorTypes(), "Id", "Description", moTor.MotorType);
            ViewBag.MotorTypes = sList;
            return PartialView("EditForm", moTor);
        }

        [HttpPost]
        public ActionResult Edit(Motorbike motor)
        {
            bool result = MotorBusiness.Update(motor);
            return RedirectToAction("Index");
            //if (result)
            //{
            //}
            //else
            //{
            //    //
            //}
            //return View();
        }

        public ActionResult Details(string plate)
        {
            Motorbike moTor = MotorBusiness.Select(plate);
            return PartialView("DetailForm", moTor);
        }
    }
}